<?php
declare (strict_types=1);

namespace app\admin\logic;

use app\admin\error\Oauth as OauthResponse;
use app\common\service\TokenService;
use think\facade\Request;
use Firebase\JWT\JWT;
use think\facade\Config;
use think\facade\Cache;

class Oauth extends AdminBase
{
    protected $url = '';

    public static $userInfo = [];

    public function checkToken(): void
    {
        $header = Request::header();
        $access_token = $header['access-token'] ?? '';
        empty($access_token) && $this->responseData(OauthResponse::$loseAccessToken);
        $tokenObj = new TokenService(['type' => 'ad', 'conValue' => '']);
        $info = $tokenObj->getAccessTokenInfo(['user_token' => $access_token]);
        self::$userInfo = json_decode(json_encode($info), true);
    }

    /**
     * 验证权限
     * @param array $param 请求参数
     * @return array
     */
    public function checkAuth(): void
    {
//        if ($this->checkWhite()) return;
//        $userInfo = self::$userInfo;
//        var_dump($userInfo);
//        die;
//        $auths = $userInfo['auths'];
//
//        !in_array($this->url, $auths) && $this->responseData(OauthError::$Unauthorized);
    }

    /**
     * 白名单，也可以不适用，后期开发人员到路由中使用中间件替代
     * @return bool
     */
    private function checkWhite()
    {
        $this->url = Request::baseUrl();
        $white_list = Config::get('white.' . Request::subDomain());
        return in_array($this->url, $white_list);
    }
}
